package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsHomeBrandMapper;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@DubboService
public class SmsHomeBrandImpl implements SmsHomeBrandApi {
    @Autowired
    private SmsHomeBrandMapper smsHomeBrandMapper;

    /**
     * 添加首页推荐品牌
     *
     * @param homeBrandList
     * @return
     */
    @Override
    public void create(List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand smsHomeBrand : homeBrandList) {
            smsHomeBrandMapper.insert(smsHomeBrand);
        }
    }

    /**
     * 批量删除推荐品牌
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        smsHomeBrandMapper.deleteBatchIds(ids);
    }

    /**
     * 分页查询推荐品牌
     *
     * @param brandName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SmsHomeBrand> list(String brandName, Integer recommendStatus, Integer pageNum, Integer pageSize) {
        IPage<SmsHomeBrand> pg = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(brandName != null, SmsHomeBrand::getBrandName, brandName);
        wrapper.like(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus);
        IPage<SmsHomeBrand> iPage = smsHomeBrandMapper.selectPage(pg, wrapper);
        return iPage;
    }

    /**
     * 批量修改推荐品牌状态
     *
     * @param recommendStatus
     * @param ids
     * @return
     */
    @Override
    public void updateStatus(Integer recommendStatus, List<Integer> ids) {
        List<SmsHomeBrand> smsHomeBrands = smsHomeBrandMapper.selectBatchIds(ids);
        for (SmsHomeBrand smsHomeBrand : smsHomeBrands) {
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeBrandMapper.updateById(smsHomeBrand);
        }
    }

    /**
     * 修改推荐品牌排序
     * @param id
     * @param sort
     * @return
     */
    @Override
    public void updateSortById(Integer id, Integer sort) {
        SmsHomeBrand smsHomeBrand = smsHomeBrandMapper.selectById(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandMapper.updateById(smsHomeBrand);
    }


}
